<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="./favicon.ico" />
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <title>Boost 搜索引擎</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            height: 100%;
        }

        .container {
            width: 800px;
            margin: 0px auto;
            margin-top: 15px;
        }

        .container .search {
            width: 100%;
            height: 52px;
        }

        .container .search input {
            /* 设置左浮动 */
            float: left;
            width: 600px;
            height: 50px;
            border: 1px solid black;
            /* border: 1px solid #4A4C4E; */
            border-right: none;
            padding-left: 10px;
            color: #9295A2;
            font-size: 15px;
        }

        .container .search button {
            float: left;
            width: 140px;
            height: 52px;
            background-color: #63855C;
            color: white;
            font-size: 17px;
            font-style: normal;
            font-family: "SF Pro SC", "SF Pro Display", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
        }

        .container .result {
            width: 100%;
            font-family: "SF Pro SC", "SF Pro Display", "SF Pro Icons", "PingFang SC", "Helvetica Neue", Helvetica, Arial, sans-serif;
        }

        .container .result .item {
            margin-top: 15px;
        }

        .container .result .item a {
            text-decoration: none;
            font-size: 20px;
            color: #63855C;
        }

        .container .result .item a:hover {
            text-decoration: underline;
        }

        .container .result .item p {
            font-size: 15px;
            margin-top: 5px;
            color: #1d1d1f;
        }

        .container .result .item i {
            font-size: 13px;
            font-style: normal;
            color: #6e6e73;
        }
    </style>
</head>

<body>
    <!-- <h1>欢迎使用Boost库搜索引擎</h1> -->
    <div class="container">
        <div class="search">
            <input type="text" value="请输入搜索内容" onfocus='if(this.value=="请输入搜索内容"){this.value="";}; '>
            <button onclick="Search()">搜索一下</button>
        </div>
        <div class="result">
            <!--
            <div class="item">
                <a herf="#">这是标题</a>
                <p>这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要这是摘要</p>
                <i>https://gitee.com/welldonexing/cpp-httplib</i>
            </div>
            -->
        </div>

    </div>
    <script>
        function Search() {
            // alert("hello js!");
            // 1. 提取数据
            let query = $(".container .search input").val();
            console.log("query: " + query);
            // 2. 发起http请求
            $.ajax({
                type: "GET",
                url: "/s?word=" + query,
                success: function (data) {
                    // console.log(data);
                    BuildHtml(data);
                }
            })
        }
        function BuildHtml(data) {
            // 获取result标签
            let tag_result = $(".container .result");
            // 清空历史结果
            tag_result.empty();

            // 想标签内添加数据
            for (let elem of data) {
                let tag_a = $("<a>", {
                    text: elem.title,
                    href: elem.url,
                    target: "_blank"
                });
                let tag_p = $("<p>", {
                    text: elem.desc
                });
                let tag_i = $("<i>", {
                    text: elem.url
                });
                let tag_div = $("<div>", {
                    class: "item"
                });

                tag_a.appendTo(tag_div);
                tag_p.appendTo(tag_div);
                tag_i.appendTo(tag_div);

                tag_div.appendTo(tag_result);
            }
        }
    </script>
</body>

</html>